package com.common.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.account.entity.Tenant;
import com.common.account.entity.UserAndRole;
import com.common.account.entity.vo.TenantStatist;
import com.common.account.mapper.TenantMapper;
import com.common.account.service.IAuUserService;
import com.common.account.service.ITenantService;
import com.common.account.service.IUserAndRoleService;
import com.common.basis.exception.BusinessException;
import com.common.basis.oauth.AuUser;
import com.common.basis.util.StringMappingTool;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 租户信息 服务实现类
 * </p>
 *
 * @author mdc
 * @since 2021-10-29
 */
@Service
@AllArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    private final IAuUserService iAuUserService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    private final IUserAndRoleService iUserAndRoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modificationApproved(Tenant param) {
        boolean b;
        Tenant byId = this.getById(param.getId());
        if (byId == null) {
            return Boolean.FALSE;
        }
        LambdaQueryWrapper<AuUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AuUser::getUserName, byId.getLoginName());
        int count = iAuUserService.count(wrapper);
        if (count > 0) {
            return Boolean.FALSE;
        }
        //修改审核通过
        LambdaUpdateWrapper<Tenant> wrapper1 = Wrappers.lambdaUpdate();
        wrapper1.eq(Tenant::getId, param.getId())
                .set(Tenant::getStatus, StringMappingTool.EXAMINATION_PASSED)
                .set(Tenant::getReviewer, param.getReviewer())
                .set(Tenant::getRemark, param.getRemark());
        b = this.update(wrapper1);
        if (b) {
            //新增用户
            AuUser auUser = new AuUser();
            auUser.setTenantId(param.getId());
            auUser.setUserName(byId.getLoginName());
            auUser.setActualName(byId.getOfficeName());
            auUser.setPhone(byId.getPhone());
            auUser.setPassword(bCryptPasswordEncoder.encode("123456"));
            auUser.setOpenId(byId.getOpenId());
            b = iAuUserService.save(auUser);
            if (b) {
                //绑定角色
                UserAndRole andRole = new UserAndRole();
                andRole.setUserId(auUser.getId());
                andRole.setRoleId(StringMappingTool.ENTERPRISE_MANAGEMENT_AUTHORITY);
                b = iUserAndRoleService.save(andRole);
            }
        }
        if (!b) {
            throw new BusinessException("modificationApproved 失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public TenantStatist statisticalAnalysis() {
        int count = count();
        TenantStatist tenantStatist = new TenantStatist();
        tenantStatist.setSum(count);
        LambdaQueryWrapper<Tenant> query = Wrappers.lambdaQuery();
        query.apply("CONCAT(YEAR(created),MONTH(created))=CONCAT(YEAR(now()),MONTH(now()))");
        int newMonth = this.count(query);
        tenantStatist.setNewMonth(newMonth);
        query.clear();
        query.apply("CONCAT(YEAR(created),MONTH(created))=CONCAT(YEAR(now()),MONTH(now())-1)");
        int addedLastMonth = this.count(query);
        tenantStatist.setAddedLastMonth(addedLastMonth);
        return tenantStatist;
    }
}
